package educar.controllers;

import educar.gui.IView;
import educar.gui.student.DeleteRollMateria;
import educar.idioma.Locale;
import educar.models.Cursa;
import educar.models.Materia;
import educar.models.Student;
import educar.models.cursaNotFound;

/**
 * Permite al alumno desvincularse de una materia
 */
public class DeleteEnrollMateriaController implements IController {
	private DeleteRollMateria view;
	private Student alumno;
	
	/**
	 * Constructor
	 * @param alumno que se desvincula de una materia
	 */
	public DeleteEnrollMateriaController(Student alumno) {
		this.alumno = alumno;
	}

	public void process(String model){
		if(null!=view) {
			/*obtengo elementos y compruebo validez*/
			Materia materia = view.getMateria();			
			if(materia==null ) {
				view.presentWarning(Locale.EMPTY_FIELDS, Locale.ERROR);
			}
			else {
					try {
						Cursa.getCursaByStudentAndMateria(alumno.getDni(),materia.getCodigo()).destroy();
						view.present(Locale.STUDENT+" "+Locale.UNROLL);						
					} catch (cursaNotFound e) {
						view.present(Locale.ERROR);
					}
					view.close();
			}
		}
	}
	
	
	public void setView(IView view) {
		this.view = (DeleteRollMateria) view;		
	}	
}

